//
//  loginView.m
//  garbageclient
//
//  Created by Wang Wu on 11-9-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "loginView.h"
#import "commondef.h"

#import "XMLRPCResponse.h"
#import "XMLRPCRequest.h"
#import "XMLRPCConnection.h"
#import "UserSingleton.h"
#import "userdata.h"

@implementation loginView

@synthesize delegate;

- (void)dealloc
{
    [navBar release];
    [namelabel release];
    [pslabel release];
    [nametext release];
    [pstext release];
    [loginbtn release];
    [registerbtn release];
    [forgatbtn release];
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        self.backgroundColor = [UIColor whiteColor];
        
        CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
        
        navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, screenrc.size.width, NAV_HEIGHT)];
        
        [self addSubview: navBar];
        
        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @"登陆"];
        [navBar pushNavigationItem:navItem animated:YES];
        
        [navItem release];
        
        namelabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, NAV_HEIGHT, 100.0f, 60.0f)];
        namelabel.text = @"用户名";
        [self addSubview:namelabel];
        
        pslabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, NAV_HEIGHT + 60.0f, 100.0f, 60.0f)];
        pslabel.text = @"密码";
        [self addSubview:pslabel];
        
        nametext = [[UITextField alloc] initWithFrame:CGRectMake(100.0f, NAV_HEIGHT, 
                                                                 screenrc.size.width - 100.0f, 
                                                                 60.0f)];
        nametext.delegate = self;
        [self addSubview:nametext];
        
        pstext = [[UITextField alloc] initWithFrame:CGRectMake(100.0f, NAV_HEIGHT + 60.0f, 
                                                               screenrc.size.width - 100.0f, 
                                                               60.0f)];
        pstext.delegate = self;
        [self addSubview:pstext];
        
        loginbtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, NAV_HEIGHT + 120.0f, 
                                                              100.0f, 60.0f)];
        [loginbtn setTitle:@"登陆" forState:UIControlStateNormal];
        [loginbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [loginbtn addTarget:self action:@selector(loginclick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loginbtn];
        
        registerbtn = [[UIButton alloc] initWithFrame:CGRectMake(100.0f, NAV_HEIGHT + 120.0f, 
                                                              100.0f, 60.0f)];
        [registerbtn setTitle:@"注册" forState:UIControlStateNormal];
        [registerbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [registerbtn addTarget:self action:@selector(registerclick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:registerbtn];
        
        forgatbtn = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, NAV_HEIGHT + 220.0f, 
                                                              100.0f, 60.0f)];
        [forgatbtn setTitle:@"忘记密码" forState:UIControlStateNormal];
        [forgatbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [forgatbtn addTarget:self action:@selector(forgatclick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:forgatbtn];
        
        webdatabuf = [[NSMutableData alloc] initWithLength:0];
    }
    return self;
}

#pragma mark-textDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [nametext resignFirstResponder];
    [pstext resignFirstResponder];
    return YES;
}

#pragma mark-button click
-(void)loginclick:(id)sender
{
    if ([nametext.text length] == 0) 
    {
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert" 
                                                          message:@"用户名不可为空" 
                                                         delegate:self
                                                cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [myAlert show];
    }
    else if([pstext.text length] == 0)
    {
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Alert" 
                                                          message:@"密码不可为空" 
                                                         delegate:self
                                                cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [myAlert show];
    }
    else
    {
        XMLRPCRequest *reqHello = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:@"http://www.mwinphone.com/xmlrpc.php"]];
        [reqHello setMethod:@"wp.getUsersBlogs" withObjects:[NSArray arrayWithObjects:nametext.text,pstext.text,nil]]; 
        
        xmlconnect = [[XMLRPCConnection alloc] initWithXMLRPCRequest:reqHello delegate:self];
    }
}

-(void)registerclick:(id)sender
{
    [delegate IsEnableBtn:NO];
    
    CGRect screenrc = [[UIScreen mainScreen] applicationFrame];
    screenrc.origin.y = 0;
    regview = [[registerView alloc] initWithFrame:screenrc];
    regview.delegate = self;
    [self addSubview:regview];
}

-(void)forgatclick:(id)sender
{
    
}

#pragma mark-alertdelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.title == @"恭喜") 
    {
        [delegate  TurnToInfo];
    }
    [alertView release];
}

#pragma mark- xmldelegate
- (void)connection:(id)connection didReceiveResponse:(XMLRPCResponse*) response forMethod:(NSString*) method
{
    [xmlconnect release];
    NSString    *faultstr = [response fault]; 
    if(faultstr != nil)
    {
        nametext.text = @"";
        pstext.text = @"";
        
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"警告" 
                                                          message:faultstr
                                                         delegate:self
                                                cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [myAlert show];
    }
    else
    {
        [self saveUserData];
        
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"恭喜" 
                                                          message:@"登陆成功" 
                                                         delegate:self
                                                cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [myAlert show];
    }
}

#pragma mark -registerDelegate
-(void) registerback
{
    [regview removeFromSuperview];
    [regview release];
    
    [delegate IsEnableBtn:YES];
}

#pragma mark -selffunction
-(void)saveUserData
{
    [UserSingleton sharedSingleton].curUserName = nametext.text;
    [UserSingleton sharedSingleton].curUserPs = pstext.text;
    [UserSingleton sharedSingleton].bhasUser = YES;
    
    userdata    *ud = [[userdata alloc] init];
    ud.strcurUserName = [UserSingleton sharedSingleton].curUserName;
    ud.strcurUserPs = [UserSingleton sharedSingleton].curUserPs;
    
    NSMutableData   *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [archiver encodeObject:ud forKey:kDataKey];
    [archiver finishEncoding];    
    [data writeToFile:[UserSingleton sharedSingleton].filePath atomically:YES];
    [ud release];
    [archiver release];
    [data release];
}
@end
